package com.cn.controller;
import com.cn.pojo.Category;
import com.cn.response.Result;
import com.cn.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能描述: <br>
 *
 * @program: wx-nimi-bank
 * @description: 菜单分类控制层
 * @author: Mr.TYJ
 * @create: 2022-05-26 17:47
 **/
@RestController
@CrossOrigin
@Slf4j
@Api(tags = "菜单分类控制层")
@RequestMapping("/catTree")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiModelProperty(value = "加载菜单树",notes = "获取所有菜单树，以及展开的子集d")
    @GetMapping("/categoryTree")
    public Result categoryTree(){
        //查询所有的菜单
        List<Category> menuTree=categoryService.findMenuTree();
        return Result.ok().data("tree",menuTree);
    }


    @ApiModelProperty(value = "查询",notes = "根据菜单分类name模糊查询")
        @PostMapping("/selectAllByName")
    public Result selectAllByName(@RequestParam(value = "name",required = false) String name ){

        List<Category> categoryList=categoryService.selectAllByName(name);
        return Result.ok().data("categoryList",categoryList);
    }

    @ApiModelProperty(value = "查询",notes = "根据菜单分类name模糊查询")
    @PostMapping("/insert")
    public Result insert(@RequestBody Category category ){

        Integer num =categoryService.insert(category);
        if (num>0){
            return Result.ok().data("code",200);
        }else {
            return Result.ok().data("code",0);
        }
    }
    @ApiModelProperty(value = "查询",notes = "根据菜单分类name模糊查询")
    @PostMapping("/update")
    public Result update(@RequestBody Category category ){

        Integer num =categoryService.update(category);
        if (num>0){
            return Result.ok().data("code",200);
        }else if (num== -1){
            return Result.ok().data("code",1);
        }else {
            return Result.ok().data("code",0);
        }
    }
    @ApiModelProperty(value = "查询",notes = "根据菜单分类name模糊查询")
    @PostMapping("/selectById")
    public Result selectById(@RequestParam(value = "id",required = true) Long id ){

        Category category=categoryService.selectById(id);
        return Result.ok().data("category",category);
    }

    @ApiModelProperty(value = "查询name",notes = "根据菜单分类name模糊查询")
    @PostMapping("/deleteById")
    public Result deleteById(@RequestParam(value = "id",required = true) Long id ){

        Integer num =categoryService.delete(id);
        if (num>0){
            return Result.ok().data("code",200);
        }else {
            return Result.ok().data("code",0);
        }
    }



}
